package db.seed;

import com.mai4j.maiadmin.setup.Helper;
import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

import java.util.LinkedHashMap;
import java.util.Map;

import static com.mai4j.maiadmin.generated.Tables.SETTING;
import static org.jooq.impl.DSL.using;


/**
 * 初始化国家和地区
 */
@Slf4j
public class V20230409174906__Insert_Setting extends BaseJavaMigration {


    static final Map<String, String> DEFAULT_SETTING = new LinkedHashMap<>();

    static {
        DEFAULT_SETTING.put("mail.engine", "smtp");
        DEFAULT_SETTING.put("mail.ssl", "true");
        DEFAULT_SETTING.put("mail.timeout", "60");
        DEFAULT_SETTING.put("mail.hostname", "smtp.gmail.com");
        DEFAULT_SETTING.put("metaDescription.en-US", "");
        DEFAULT_SETTING.put("language.catalog", "zh-CN");
        DEFAULT_SETTING.put("siteTitle.zh-CN", "Mai admin - 极速开发后端模板");
        DEFAULT_SETTING.put("mail.password", "000000");
        DEFAULT_SETTING.put("metaTitle.zh-CN", "Mai admin - 极速开发后端模板");
        DEFAULT_SETTING.put("mail.personal", "Mai admin");
        DEFAULT_SETTING.put("metaKeyword.zh-CN", "");
        DEFAULT_SETTING.put("mail.port", "443");
        DEFAULT_SETTING.put("metaKeyword.en-US", "");
        DEFAULT_SETTING.put("encryption", Helper.rand(256));
        DEFAULT_SETTING.put("language.admin", "zh-CN");
        DEFAULT_SETTING.put("siteTitle.en-US", "Mai admin - Rapid admin template");
        DEFAULT_SETTING.put("metaDescription.zh-CN", "");
        DEFAULT_SETTING.put("currency", "CNY");
        DEFAULT_SETTING.put("mail.username", "example@gmail.com");
        DEFAULT_SETTING.put("metaTitle.en-US", "Mai admin - Rapid admin template");
        DEFAULT_SETTING.put("maintenance", "false");

    }

    @Override
    public void migrate(Context context) {
        for (Map.Entry<String, String> entry : DEFAULT_SETTING.entrySet()) {
            using(context.getConnection()).insertInto(SETTING).columns(
                    SETTING.SETTING_ID,
                    SETTING.CODE,
                    SETTING.NAME,
                    SETTING.VALUE).values(
                    Helper.next(),
                    "config",
                    entry.getKey(),
                    entry.getValue()).execute();
        }
    }
}
